/*
 * MainWindow.java
 *
 * Created on May 20, 2006, 12:50 PM
 */

/**
 *
 * @author  Timoz
 */
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.concurrent.*;
import java.sql.*;
import javax.print.*;
import java.awt.print.*;

public class MainWindow extends javax.swing.JFrame {
    //Tree Variables
    private DefaultTreeModel model;
    private DefaultMutableTreeNode root;
    static int recordsLimit = 5, undoHistoryLimit=100, gridSpacing=50;
    
    protected static DBConnector dbCon;  //Connection to the database
    private Vector myDatabases = new Vector();  //a Vector to store the databases found in SQL server
    protected static Vector<String> myForms = new Vector<String>();  //a Vector to store the forms belonging to a selected database
    private String selectedForm = new String();  //String to store the currently selected form
    protected static String selectedDatabase=new String(); //String to store the currently selected database
    protected String formTitle;  //String to store the default Form Name (Form 1, Form 2, etc)
    protected String copyForm;  //String to store the Form Name which the user has selected to copy
    protected static ArrayList<MyComponentContainer> copiedComponents=new ArrayList<MyComponentContainer>();  //Array List to store the copied or cut components until they are pasted
    protected static boolean flagCut=false;  //Boolean to store if the copied components were cut or copied (true if they were cut)
    int selectedIndex;  //Integer to get the index of the selected Tree Node (Database)
    boolean flagStart=true;
    
    private String username, password; 
    public static String host;
    
    /** Creates new form MainWindow */
    public MainWindow() {
        try
        {
        RandomAccessFile settingsFile = new RandomAccessFile ("settings.cfg","r");
        settingsFile.readUTF();
        recordsLimit=settingsFile.readInt();
        settingsFile.readUTF();
        undoHistoryLimit=settingsFile.readInt();
        settingsFile.readUTF();
        gridSpacing=settingsFile.readInt();
        }
        catch (IOException ex)
        {}
        
        initComponents();
        jTree1.setModel(null);
        copy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Copy.png")));
        paste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Paste.png")));
        save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Save.png")));
        openView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonOpen.png")));
        designView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Design.png")));
        print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Print.png")));
        printPreview.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/PrintPreview.png")));
        delete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Delete.png")));
        newForm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonNew.png")));
        formInDesignView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Design.png")));
        formByUsingWizard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/FormWizard.png")));
        
        //Initialize Buttons
        buttonDelete.setEnabled(false);
        buttonOpen.setEnabled(false);
        buttonDesign.setEnabled(false);
        buttonNew.setEnabled(false);
        
        //Initialize Menu
        byFirstCreated.setEnabled(false);
        byLastModified.setEnabled(false);
        byName.setEnabled(false);
        copy.setEnabled(false);
        delete.setEnabled(false);
        designView.setEnabled(false);
        export.setEnabled(false);
        formByUsingWizard.setEnabled(false);
        formInDesignView.setEnabled(false);
        newForm.setEnabled(false);
        openView.setEnabled(false);
        paste.setEnabled(false);
        print.setEnabled(false);
        printPreview.setEnabled(false);
        rename.setEnabled(false);
        save.setEnabled(false);
        
        //Get the databases and insert them into the tree
        selectedDatabase="";
        
        addWindowListener( new WindowHandler());
        
        //Initialize the JList
        jList1.setLayoutOrientation(JList.VERTICAL_WRAP);  //set the layout of the JList to columns according to screen dimensions
        
        //Initialize the Frame
        setTitle("MYSQL Form Builder v1.0");
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        buttonOpen = new javax.swing.JButton();
        buttonDesign = new javax.swing.JButton();
        buttonNew = new javax.swing.JButton();
        buttonDelete = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        File = new javax.swing.JMenu();
        save = new javax.swing.JMenuItem();
        export = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        printPreview = new javax.swing.JMenuItem();
        print = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        exit = new javax.swing.JMenuItem();
        Edit = new javax.swing.JMenu();
        copy = new javax.swing.JMenuItem();
        paste = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        delete = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        rename = new javax.swing.JMenuItem();
        View = new javax.swing.JMenu();
        openView = new javax.swing.JMenuItem();
        designView = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        arrange = new javax.swing.JMenu();
        byName = new javax.swing.JMenuItem();
        byFirstCreated = new javax.swing.JMenuItem();
        byLastModified = new javax.swing.JMenuItem();
        Insert = new javax.swing.JMenu();
        newForm = new javax.swing.JMenuItem();
        formInDesignView = new javax.swing.JMenuItem();
        formByUsingWizard = new javax.swing.JMenuItem();
        Options = new javax.swing.JMenu();
        Settings = new javax.swing.JMenuItem();
        Help = new javax.swing.JMenu();
        about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTree1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jTree1);

        jList1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setVisibleRowCount(0);
        jList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jList1KeyPressed(evt);
            }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jList1MousePressed(evt);
            }
        });

        jScrollPane2.setViewportView(jList1);

        buttonOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonOpen.png")));
        buttonOpen.setText("Open");
        buttonOpen.setToolTipText("Open Form");
        buttonOpen.setIconTextGap(5);
        buttonOpen.setMargin(new java.awt.Insets(2, 6, 2, 10));
        buttonOpen.setPreferredSize(new java.awt.Dimension(67, 27));
        buttonOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOpenActionPerformed(evt);
            }
        });

        buttonDesign.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Design.png")));
        buttonDesign.setText("Design");
        buttonDesign.setToolTipText("Design Form");
        buttonDesign.setIconTextGap(5);
        buttonDesign.setMargin(new java.awt.Insets(2, 6, 2, 10));
        buttonDesign.setPreferredSize(new java.awt.Dimension(73, 27));
        buttonDesign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDesignActionPerformed(evt);
            }
        });

        buttonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonNew.png")));
        buttonNew.setText("New");
        buttonNew.setToolTipText("New Form");
        buttonNew.setIconTextGap(5);
        buttonNew.setMargin(new java.awt.Insets(2, 6, 2, 10));
        buttonNew.setPreferredSize(new java.awt.Dimension(61, 27));
        buttonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNewActionPerformed(evt);
            }
        });

        buttonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Delete.png")));
        buttonDelete.setText("Delete");
        buttonDelete.setToolTipText("Delete Form");
        buttonDelete.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        buttonDelete.setIconTextGap(5);
        buttonDelete.setMargin(new java.awt.Insets(2, 6, 2, 10));
        buttonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(buttonOpen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(buttonDesign, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(buttonNew, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                    .add(buttonDelete))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(buttonOpen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(buttonDesign, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(buttonNew, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(buttonDelete, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
                .addContainerGap())
        );

        File.setMnemonic('F');
        File.setText("File");
        File.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileActionPerformed(evt);
            }
        });

        save.setMnemonic('S');
        save.setText("Save As...  (CTRL+S)");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        File.add(save);

        export.setMnemonic('E');
        export.setText("       Export...  (CTRL+E)");
        export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportActionPerformed(evt);
            }
        });

        File.add(export);

        File.add(jSeparator1);

        printPreview.setMnemonic('v');
        printPreview.setText(" Print Preview");
        File.add(printPreview);

        print.setMnemonic('P');
        print.setText(" Print  (CTRL+P)");
        print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printActionPerformed(evt);
            }
        });
        print.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                printMouseClicked(evt);
            }
        });

        File.add(print);

        File.add(jSeparator2);

        exit.setMnemonic('x');
        exit.setText("       Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        File.add(exit);

        jMenuBar.add(File);

        Edit.setMnemonic('E');
        Edit.setText("Edit");
        Edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditActionPerformed(evt);
            }
        });

        copy.setMnemonic('C');
        copy.setText("Copy  (CTRL+C)");
        copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyActionPerformed(evt);
            }
        });

        Edit.add(copy);

        paste.setMnemonic('P');
        paste.setText("Paste  (CTRL+V)");
        paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasteActionPerformed(evt);
            }
        });

        Edit.add(paste);

        Edit.add(jSeparator3);

        delete.setMnemonic('D');
        delete.setText(" Delete  (DELETE)");
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });

        Edit.add(delete);

        Edit.add(jSeparator4);

        rename.setMnemonic('R');
        rename.setText("       Rename  (CTRL+R)");
        rename.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameActionPerformed(evt);
            }
        });

        Edit.add(rename);

        jMenuBar.add(Edit);

        View.setMnemonic('V');
        View.setText("View");
        View.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ViewActionPerformed(evt);
            }
        });

        openView.setMnemonic('O');
        openView.setText("Open View  (CTRL+O)");
        openView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openViewActionPerformed(evt);
            }
        });

        View.add(openView);

        designView.setMnemonic('D');
        designView.setText("Design View  (CTRL+D)");
        designView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                designViewActionPerformed(evt);
            }
        });

        View.add(designView);

        View.add(jSeparator5);

        arrange.setMnemonic('a');
        arrange.setText("       Arrange");
        arrange.setActionCommand("     Arrange");
        byName.setMnemonic('N');
        byName.setText("By Name");
        byName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                byNameActionPerformed(evt);
            }
        });

        arrange.add(byName);

        byFirstCreated.setMnemonic('C');
        byFirstCreated.setText("By First Created");
        byFirstCreated.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                byFirstCreatedActionPerformed(evt);
            }
        });

        arrange.add(byFirstCreated);

        byLastModified.setMnemonic('M');
        byLastModified.setText("By Last Modified");
        byLastModified.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                byLastModifiedActionPerformed(evt);
            }
        });

        arrange.add(byLastModified);

        View.add(arrange);

        jMenuBar.add(View);

        Insert.setMnemonic('I');
        Insert.setText("Insert");
        newForm.setMnemonic('N');
        newForm.setText("New Form  (CTRL+N)");
        newForm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newFormActionPerformed(evt);
            }
        });

        Insert.add(newForm);

        formInDesignView.setMnemonic('V');
        formInDesignView.setText("Form in Design View");
        formInDesignView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formInDesignViewActionPerformed(evt);
            }
        });

        Insert.add(formInDesignView);

        formByUsingWizard.setMnemonic('W');
        formByUsingWizard.setText("Form by using Wizard");
        formByUsingWizard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formByUsingWizardActionPerformed(evt);
            }
        });

        Insert.add(formByUsingWizard);

        jMenuBar.add(Insert);

        Options.setMnemonic('O');
        Options.setText("Options");
        Settings.setMnemonic('S');
        Settings.setText("Settings");
        Settings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SettingsActionPerformed(evt);
            }
        });

        Options.add(Settings);

        jMenuBar.add(Options);

        Help.setMnemonic('H');
        Help.setText("Help");
        about.setMnemonic('A');
        about.setText("About MYSQL Form Builder");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });

        Help.add(about);

        jMenuBar.add(Help);

        setJMenuBar(jMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportActionPerformed
// TODO add your handling code here:
        try {
            JFileChooser chooser = new JFileChooser();
            chooser.showSaveDialog(this);
            File path = chooser.getSelectedFile();             
            RandomAccessFile file = new RandomAccessFile("export.cfg","rw");
            file.writeUTF(username);
            file.writeUTF(password);
            file.writeUTF(host);
            file.writeUTF(dbCon.getDatabase());            
            file.writeUTF(selectedForm);            
            Runtime.getRuntime().exec("jar cvfm "+path+" mainclass.mf "
                    +System.getProperty("user.dir")+"/*.class");            
            ExportForm export = new ExportForm();
        } catch(Exception ex) { ex.printStackTrace(); }
        
    }//GEN-LAST:event_exportActionPerformed

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
// TODO add your handling code here:
        new About();
    }//GEN-LAST:event_aboutActionPerformed

    private void SettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsActionPerformed
    new Settings ();
    }//GEN-LAST:event_SettingsActionPerformed
    
    private void jList1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList1KeyPressed
        String selected = (String) jList1.getSelectedValue();
        if (selected != null) {
            if (! selected.equals("Create Form by using Wizard") && ! selected.equals("Create Form in Design View")) {
                int key=evt.getKeyCode();
                if (evt.isControlDown()) {
                    switch (key) {
                        case KeyEvent.VK_C:
                            copyForm=selectedDatabase+"."+selectedForm;
                            paste.setEnabled(true);
                            break;
                        case KeyEvent.VK_V:
                            if (copyForm != null && copyForm.substring(0,copyForm.indexOf('.')).equals(selectedDatabase)) {  //if the user has copied a file set the paste item to enabled
                                paste();
                            }
                            break;
                        case KeyEvent.VK_R:
                            rename();
                            break;
                        case KeyEvent.VK_S:
                            save();
                            break;
                        case KeyEvent.VK_D:
                            designView();
                            break;
                        case KeyEvent.VK_O:
                            openView();
                            break;
                        case KeyEvent.VK_N:
                            NewFormMenu newFormMenu = new NewFormMenu();  //display a wizard from which the user can select what type of Form he wants
                            newFormMenu.setVisible(true);  //after creating a new class of NewFormMenu which extends JFrame, display the frame
                            break;
                    }
                } else {
                    switch (key) {
                        case KeyEvent.VK_DELETE:
                            int i=JOptionPane.showConfirmDialog(null,"Are You Sure You Want To Permanently Delete This Form?","Deleting",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
                            if (i==JOptionPane.YES_OPTION) {  //if the user selects yes (he's sure he wants to delete the form)
                                deleteForm();
                                buttonDelete.setEnabled(false);  //disable the delete button until the user selects another Form
                                delete.setEnabled(false);
                            }
                            break;
                            
                        case KeyEvent.VK_ENTER:
                            openView();
                            break;
                    }
                }
            } else {
                if (evt.isControlDown() && evt.getKeyCode()==KeyEvent.VK_N) {
                    NewFormMenu newFormMenu = new NewFormMenu();  //display a wizard from which the user can select what type of Form he wants
                    newFormMenu.setVisible(true);  //after creating a new class of NewFormMenu which extends JFrame, display the frame
                } else if (evt.isControlDown() && evt.getKeyCode()==KeyEvent.VK_V) {
                    if (copyForm != null && copyForm.substring(0,copyForm.indexOf('.')).equals(selectedDatabase)) {  //if the user has copied a file set the paste item to enabled
                        paste();
                    }
                }
            }
        }else {
            if (evt.isControlDown() && evt.getKeyCode()==KeyEvent.VK_N) {
                NewFormMenu newFormMenu = new NewFormMenu();  //display a wizard from which the user can select what type of Form he wants
                newFormMenu.setVisible(true);  //after creating a new class of NewFormMenu which extends JFrame, display the frame
            } else if (evt.isControlDown() && evt.getKeyCode()==KeyEvent.VK_V) {
                if (copyForm != null && copyForm.substring(0,copyForm.indexOf('.')).equals(selectedDatabase)) {  //if the user has copied a file set the paste item to enabled
                    paste();
                }
            }
        }
    }//GEN-LAST:event_jList1KeyPressed
    
    private void byLastModifiedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_byLastModifiedActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            arrangeByLastModified();
        }
    }//GEN-LAST:event_byLastModifiedActionPerformed
    
    private void byFirstCreatedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_byFirstCreatedActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            arrangeByFirstCreated();
        }
    }//GEN-LAST:event_byFirstCreatedActionPerformed
    
    private void byNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_byNameActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            arrangeByName();
        }
    }//GEN-LAST:event_byNameActionPerformed
    
    private void formInDesignViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formInDesignViewActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            createFormDesign();
        }
    }//GEN-LAST:event_formInDesignViewActionPerformed
    
    private void formByUsingWizardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formByUsingWizardActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            FormWizard formWizard = new FormWizard(myForms);
        }
    }//GEN-LAST:event_formByUsingWizardActionPerformed
    
    private void newFormActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newFormActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            NewFormMenu newFormMenu = new NewFormMenu();  //display a wizard from which the user can select what type of Form he wants
            newFormMenu.setVisible(true);  //after creating a new class of NewFormMenu which extends JFrame, display the frame
        }
    }//GEN-LAST:event_newFormActionPerformed
    
    private void designViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_designViewActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            designView();
        }
    }//GEN-LAST:event_designViewActionPerformed
    
    private void openViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openViewActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            openView();
        }
    }//GEN-LAST:event_openViewActionPerformed
    
    private void renameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            rename();
        }
    }//GEN-LAST:event_renameActionPerformed
    
    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        //display a warning message to the user
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            int i=JOptionPane.showConfirmDialog(null,"Are You Sure You Want To Permanently Delete This Form?","Deleting",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
            if (i==JOptionPane.YES_OPTION) {  //if the user selects yes (he's sure he wants to delete the form)
                deleteForm();
                buttonDelete.setEnabled(false);  //disable the delete button until the user selects another Form
                delete.setEnabled(false);
            }
        }
    }//GEN-LAST:event_deleteActionPerformed
    
    private void pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            paste();
        }
    }//GEN-LAST:event_pasteActionPerformed
    
    private void copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            copyForm=selectedDatabase+"."+selectedForm;
            paste.setEnabled(true);
        }
    }//GEN-LAST:event_copyActionPerformed
    
    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            System.exit(0);
        }
    }//GEN-LAST:event_exitActionPerformed
    
    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        if (((JMenuItem)evt.getSource()).isEnabled() && evt.getModifiers()==16) {
            save();
        }
    }//GEN-LAST:event_saveActionPerformed
    
    private void EditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_EditActionPerformed
    
    private void ViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_ViewActionPerformed
    
    private void printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printActionPerformed
// TODO add your handling code here:
    boolean flagPrint=false;
    PrinterJob printJob=PrinterJob.getPrinterJob();
    flagPrint=printJob.printDialog();
    if (flagPrint){
        MyFileChooser myFileChooser=new MyFileChooser();
        myFileChooser.printFileChooserSelection=myFileChooser.jFileChooser1.showSaveDialog(null);
    }
    }//GEN-LAST:event_printActionPerformed
    
    private void printMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_printMouseClicked
// TODO add your handling code here:
        
    }//GEN-LAST:event_printMouseClicked
    
    private void FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_FileActionPerformed
    /** Mouse Pressed Listener for the JList that holds the Forms
     * List belonging to a certain database that the user has selected
     */
    private void jList1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MousePressed
        //handle the mouse clicked event on the Forms list
        int lastIndex=myForms.size()-1;  //Integer to get the last index in the myForms Vector
        if (lastIndex != -1) {
            //get the cellbounds of the JList Cells to check if the mouse was clicked on a non empty area
            if (jList1.getCellBounds(0,lastIndex).contains(evt.getPoint())) {  //if the mouse was clicked on a cell, (Right or Left Click)
                jList1.setSelectedIndex(jList1.locationToIndex(evt.getPoint()));  //get the point at which the mouse was clicked, and translate it to an index
                selectedForm=(String)jList1.getSelectedValue();  //set the selectedForm to that cell the user clicked on
            }
        }
    }//GEN-LAST:event_jList1MousePressed
    /** Action Performed Listener for the New Button
     */
    private void buttonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNewActionPerformed
        NewFormMenu newFormMenu = new NewFormMenu();  //dispaly a wizard from which the user can select what type of Form he wants
        newFormMenu.setVisible(true);  //after creating a new class of NewFormMenu which extends JFrame, display the frame
    }//GEN-LAST:event_buttonNewActionPerformed
    /** Action Performed Listener for the Delete Button
     */
    private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
        //display a warning message to the user
        if (buttonDelete.isEnabled() & evt.getModifiers()==16) {  //check if the delete button is enabled, and the user clicked a left click
            int i=JOptionPane.showConfirmDialog(null,"Are You Sure You Want To Permanently Delete This Form?","Deleting",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
            if (i==JOptionPane.YES_OPTION) {  //if the user selects yes (he's sure he wants to delete the form)
                deleteForm();
                buttonDelete.setEnabled(false);  //disable the delete button until the user selects another Form
                delete.setEnabled(false);
            }
        }
    }//GEN-LAST:event_buttonDeleteActionPerformed
    /** Value Changed Listener for the Databases Tree
     */
    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        //Handle the tree selectedForm events
        selectedIndex=0;
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
        if (selectedNode != null && selectedNode.getParent()==null)  //if the user has selected the root node (Databases)
        {
            myForms.removeAllElements();  //Empty the myForms Vector
            jList1.setListData(myForms);  //set the Forms List to Empty
            //Disable the buttons
            buttonDelete.setEnabled(false);
            buttonNew.setEnabled(false);
            buttonOpen.setEnabled(false);
            buttonDesign.setEnabled(false);
            byFirstCreated.setEnabled(false);
            byLastModified.setEnabled(false);
            byName.setEnabled(false);
            copy.setEnabled(false);
            delete.setEnabled(false);
            designView.setEnabled(false);
            export.setEnabled(false);
            formByUsingWizard.setEnabled(false);
            formInDesignView.setEnabled(false);
            newForm.setEnabled(false);
            openView.setEnabled(false);
            paste.setEnabled(false);
            print.setEnabled(false);
            printPreview.setEnabled(false);
            rename.setEnabled(false);
            save.setEnabled(false);
        } else {
            buttonNew.setEnabled(true);
            newForm.setEnabled(true);
            byFirstCreated.setEnabled(true);
            byLastModified.setEnabled(true);
            byName.setEnabled(true);
            formInDesignView.setEnabled(true);
            formByUsingWizard.setEnabled(true);
            paste.setEnabled(false);
            
            if (selectedNode != null && selectedNode.getParent() != null) {  //if the user has selected a Database Node
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                selectedIndex = parent.getIndex(selectedNode);
                selectedDatabase=(String)myDatabases.get(selectedIndex);  //update the selectedDatabase String to store the currently selected database
                dbCon.setDatabase((String)myDatabases.get(selectedIndex));
                
                if (copyForm != null && copyForm.substring(0,copyForm.indexOf('.')).equals(selectedDatabase)) {  //if the user has copied a file set the paste item to enabled
                    paste.setEnabled(true);
                }
                //Set the Forms List according to the Database selectedForm
                setForms();
                
                buttonDelete.setEnabled(false);
                delete.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jTree1ValueChanged
    /** Mouse Clicked Listener for the Forms List
     */
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        //handle the mouse clicked event on the Forms list
        int lastIndex=myForms.size()-1;  //Integer to get the last index in the myForms Vector
        if (lastIndex != -1)  //if the myForms Vector is not empty
        {
            //get the cellbounds of the JList Cells to check if the mouse was clicked on an empty area
            if (! jList1.getCellBounds(0,lastIndex).contains(evt.getPoint())) {  //if the user has clicked on an empty area
                jList1.clearSelection();  //Clear the form selection
                selectedForm="";
                buttonDelete.setEnabled(false);
                buttonOpen.setEnabled(false);
                buttonDesign.setEnabled(false);
                copy.setEnabled(false);
                delete.setEnabled(false);
                designView.setEnabled(false);
                export.setEnabled(false);
                openView.setEnabled(false);
                print.setEnabled(false);
                printPreview.setEnabled(false);
                rename.setEnabled(false);
                save.setEnabled(false);
                if (evt.getButton()==evt.BUTTON3 & evt.getClickCount()==1) {  //if the user right clicked
                    //display the PopUp Menu
                    JPopupMenu popMenu = new JPopupMenu();
                    JMenuItem pasteItem =new JMenuItem("Paste");
                    pasteItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Paste.png")));
                    JMenu arrangeItem =new JMenu("      Arrange");
                    JMenuItem byNameItem = new JMenuItem("By Name");
                    JMenuItem byCreatedItem = new JMenuItem("By First Created");
                    JMenuItem byModifiedItem = new JMenuItem("By Last Modified");
                    EmptyselectedFormPopUpMenuHandler popHandler = new EmptyselectedFormPopUpMenuHandler();
                    pasteItem.addActionListener(popHandler);
                    byNameItem.addActionListener(popHandler);
                    byCreatedItem.addActionListener(popHandler);
                    byModifiedItem.addActionListener(popHandler);
                    arrangeItem.add(byNameItem);
                    arrangeItem.add(byCreatedItem);
                    arrangeItem.add(byModifiedItem);
                    popMenu.add(arrangeItem);
                    popMenu.addSeparator();
                    popMenu.add(pasteItem);
                    popMenu.setPopupSize(100,60);
                    pasteItem.setEnabled(false);
                    if (copyForm != null && copyForm.substring(0,copyForm.indexOf('.')).equals(selectedDatabase)) {  //if the user has copied a file set the paste item to enabled
                        pasteItem.setEnabled(true);
                    }
                    popMenu.show(jList1,evt.getX(),evt.getY());
                }
            } else {  //if the mouse was clicked on a Cell
                int selectedIndex = jList1.getSelectedIndex();
                boolean clickWithinCellBounds=jList1.getCellBounds(selectedIndex,selectedIndex).contains(evt.getPoint());  //Check that the clicks were within the cell boundaries
                if (evt.getButton()==evt.BUTTON1 & evt.getClickCount()==2 & clickWithinCellBounds) {  //if the user has double left clicked and the clicks were within the cell boundaries
                    if (selectedForm.equals("Create Form by using Wizard")) {  //if the selectedForm was to create a form by wizard
                        FormWizard formWizard = new FormWizard(myForms);  //display a new Form Wizard
                    } else if (selectedForm.equals("Create Form in Design View")) {  //if the selectedForm was to create a form in design view
                        createFormDesign();
                    } else {
                        OpenView.openView(selectedForm);
                    }
                } else if (evt.getButton()==evt.BUTTON3 & evt.getClickCount()==1 & clickWithinCellBounds) {  //if the user right clicked on a Cell
                    //display the PopUp Menu
                    JPopupMenu popMenu = new JPopupMenu();
                    JMenuItem copyItem =new JMenuItem("Copy");
                    copyItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Copy.png")));
                    JMenuItem openItem =new JMenuItem("Open View");
                    openItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonOpen.png")));
                    JMenuItem designItem =new JMenuItem("Design View");
                    designItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Design.png")));
                    JMenuItem printItem =new JMenuItem("Print");
                    printItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Print.png")));
                    JMenuItem printPreviewItem =new JMenuItem("Print Preview");
                    printPreviewItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/PrintPreview.png")));
                    JMenuItem saveAsItem =new JMenuItem("      Save As...");
                    JMenuItem exportItem =new JMenuItem("      Export...");
                    JMenuItem deleteItem =new JMenuItem("Delete");
                    deleteItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Delete.png")));
                    JMenuItem renameItem =new JMenuItem("      Rename");
                    SelectedFormPopupMenuHandler popHandler = new SelectedFormPopupMenuHandler();
                    copyItem.addActionListener(popHandler);
                    openItem.addActionListener(popHandler);
                    designItem.addActionListener(popHandler);
                    printItem.addActionListener(popHandler);
                    printPreviewItem.addActionListener(popHandler);
                    saveAsItem.addActionListener(popHandler);
                    exportItem.addActionListener(popHandler);
                    deleteItem.addActionListener(popHandler);
                    renameItem.addActionListener(popHandler);
                    popMenu.add(openItem);
                    popMenu.add(designItem);
                    popMenu.add(printItem);
                    popMenu.add(printPreviewItem);
                    popMenu.addSeparator();
                    popMenu.add(copyItem);
                    popMenu.add(saveAsItem);
                    popMenu.add(exportItem);
                    popMenu.addSeparator();
                    popMenu.add(deleteItem);
                    popMenu.add(renameItem);
                    popMenu.setPopupSize(130,250);
                    //Enable only the design and open buttons for "Create Form in Design View" and "Create Form by wizard"
                    if (jList1.getSelectedIndex()==0 || jList1.getSelectedIndex()==1) {
                        copyItem.setEnabled(false);
                        printItem.setEnabled(false);
                        printPreviewItem.setEnabled(false);
                        saveAsItem.setEnabled(false);
                        exportItem.setEnabled(false);
                        deleteItem.setEnabled(false);
                        renameItem.setEnabled(false);
                    }
                    popMenu.show(jList1,evt.getX(),evt.getY());
                }
            }
        }
    }//GEN-LAST:event_jList1MouseClicked
    /** Action Performed Listener For The Design Button
     */
    private void buttonDesignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDesignActionPerformed
        designView();
    }//GEN-LAST:event_buttonDesignActionPerformed
    
    private void buttonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOpenActionPerformed
        openView();
    }//GEN-LAST:event_buttonOpenActionPerformed
    
    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        selectedForm = (String) jList1.getSelectedValue();
        if (selectedForm != null) {
            if (selectedForm.equals("Create Form by using Wizard") || selectedForm.equals("Create Form in Design View")) {
                buttonDelete.setEnabled(false);
                buttonOpen.setEnabled(true);
                buttonDesign.setEnabled(true);
                copy.setEnabled(false);
                delete.setEnabled(false);
                designView.setEnabled(true);
                export.setEnabled(false);
                openView.setEnabled(true);
                print.setEnabled(false);
                printPreview.setEnabled(false);
                rename.setEnabled(false);
                save.setEnabled(false);
            } else {
                buttonDelete.setEnabled(true);
                buttonDelete.setEnabled(true);
                buttonOpen.setEnabled(true);
                buttonDesign.setEnabled(true);
                copy.setEnabled(true);
                delete.setEnabled(true);
                designView.setEnabled(true);
                export.setEnabled(true);
                openView.setEnabled(true);
                print.setEnabled(true);
                printPreview.setEnabled(true);
                rename.setEnabled(true);
                save.setEnabled(true);
            }
        }
    }//GEN-LAST:event_jList1ValueChanged
    
    /**
     * Method to initialize the Tree by setting the root to 'My Databases'
     */
    
    public DefaultMutableTreeNode makeTree() {
        DefaultMutableTreeNode root2 = new DefaultMutableTreeNode("My Databases");
        return root2;
    }
    
    /**
     * Method to refresh the Forms List whenever a new Form is created
     */
    
    public static void refreshFormList(String formTitle) {
        myForms.add(formTitle);
        jList1.setListData(myForms);
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainWindow win = new MainWindow();
                win.setVisible(true);
                win.new Login();
            }
        });
    }
    
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Pop Up Menu Handler^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    /**
     *Action Listener for the Menu that pops up when the user right clicks on a Cell
     *in the Forms List.
     */
    
    class SelectedFormPopupMenuHandler implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            if (((JMenuItem)a.getSource()).isEnabled() & a.getModifiers()==16) {
                if (a.getActionCommand().equals("Copy")) {
                    copyForm=selectedDatabase+"."+selectedForm;
                    paste.setEnabled(true);
                } else if (a.getActionCommand().equals("      Rename")) {
                    rename();
                } else if (a.getActionCommand().equals("Delete")) {
                    int i=JOptionPane.showConfirmDialog(null,"Are You Sure You Want To Permanently Delete This Form?","Deleting",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
                    if (i==JOptionPane.YES_OPTION) {
//Remove the File
                        String curDir = System.getProperty("user.dir");
                        boolean success = (new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+(String)jList1.getSelectedValue())).delete();
                        if (!success) {
                            // Deletion failed
                        }
                        
//Remove the selected form from the list
                        myForms.remove(jList1.getSelectedIndex());
                        jList1.setListData(myForms);
                        buttonDelete.setEnabled(false);
                        buttonOpen.setEnabled(false);
                        buttonDesign.setEnabled(false);
                        copy.setEnabled(false);
                        delete.setEnabled(false);
                        designView.setEnabled(false);
                        export.setEnabled(false);
                        openView.setEnabled(false);
                        print.setEnabled(false);
                        printPreview.setEnabled(false);
                        rename.setEnabled(false);
                        save.setEnabled(false);
                    }
                } else if (a.getActionCommand().equals("      Save As...")) {
                    save();
                }else if (a.getActionCommand().equals("Open View")){
                    openView();
                } else if (a.getActionCommand().equals("Design View")){
                    designView();
                }
            }
        }
    }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^End Of Pop Up Menu Handler^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//--------------------------------------------Empty Space Pop Up Menu Handler----------------------
    /** Action Listener for the Menu that pops up when the user right clicks on an empty area
     *in the Forms List.
     */
    
    class EmptyselectedFormPopUpMenuHandler implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            if (((JMenuItem)a.getSource()).isEnabled()) {
                if (a.getActionCommand().equals("Paste")) {
                    paste();
                } else if (a.getActionCommand().equals("By Name")) {
                    arrangeByName();
                } else if (a.getActionCommand().equals("By First Created")) {
                    arrangeByFirstCreated();
                } else if (a.getActionCommand().equals("By Last Modified")) {
                    arrangeByLastModified();
                }
            }
        }
    }
//-----------------------------------------End Of Empty Space Pop Up Menu Handler------------------
    
    /** Method that copies a source file to a destination file.
     * If the destination file file doesn't exist it is created.
     */
    
    protected void copyForm(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
        
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    
    protected void deleteForm() {
        //Remove the File
        String curDir = System.getProperty("user.dir");  //get the directory from which the program was run
        boolean success = (new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+(String)jList1.getSelectedValue())).delete();  //delete the file
        if (!success) {
            // Deletion failed
        }
        
        //Remove the selected form from the list
        myForms.remove(jList1.getSelectedIndex());  //remove the file name from myForms Vector
        jList1.setListData(myForms);  //set the Forms List data to the updated myForms Vector
    }
    
    protected void paste() {
        String curDir = System.getProperty("user.dir");
        String copyName=copyForm.substring(copyForm.indexOf('.')+1);
        copyName="Copy Of " + copyName;
        while (true) {
            boolean flagNameExists=false;
            for (String str : myForms) {
                if (copyName.equals(str)) {
                    flagNameExists=true;
                }
            }
            if (flagNameExists) {
                copyName="Copy Of " + copyName;
            } else {
                break;
            }
        }
        File sourceFile = new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+copyForm.substring(copyForm.indexOf('.')+1));
        File destinationFile = new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+copyName);
        try {
            copyForm(sourceFile,destinationFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        myForms.add(copyName);
        jList1.setListData(myForms);
        
        copy.setEnabled(false);
        delete.setEnabled(false);
        designView.setEnabled(false);
        export.setEnabled(false);
        openView.setEnabled(false);
        print.setEnabled(false);
        printPreview.setEnabled(false);
        rename.setEnabled(false);
        save.setEnabled(false);
    }
    
    protected void rename() {
        String curDir = System.getProperty("user.dir");
        String newName = JOptionPane.showInputDialog("Enter Form's New Name:");
        if (newName!=null) {
            boolean flagNameExists=false;
            for (String str : myForms) {
                if (newName.equals(str)) {
                    flagNameExists=true;
                }
            }
            if (flagNameExists) {
                JOptionPane.showMessageDialog(MainWindow.this,"A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
            } else if (newName.equals("")) {
                JOptionPane.showMessageDialog(MainWindow.this,"Form Name Cannot Be Blank!");
            } else {
                File file = new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+selectedForm);
                file.renameTo(new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+newName));
                file.delete();
                myForms.remove(jList1.getSelectedIndex());
                myForms.add(newName);
                jList1.setListData(myForms);
            }
        }
        buttonDelete.setEnabled(false);
        buttonOpen.setEnabled(false);
        buttonDesign.setEnabled(false);
        copy.setEnabled(false);
        delete.setEnabled(false);
        designView.setEnabled(false);
        export.setEnabled(false);
        openView.setEnabled(false);
        print.setEnabled(false);
        printPreview.setEnabled(false);
        rename.setEnabled(false);
        save.setEnabled(false);
    }
    
    protected void save() {
        String curDir = System.getProperty("user.dir");
        String saveAsName = JOptionPane.showInputDialog("Save Form "+selectedForm+" To:","Copy Of "+selectedForm);
        if (saveAsName!=null) {
            boolean flagNameExists=false;
            for (String str : myForms) {
                if (saveAsName.equals(str)) {
                    flagNameExists=true;
                }
            }
            if (flagNameExists) {
                JOptionPane.showMessageDialog(MainWindow.this,"A Form With The Name You Specified Already Exists!\nPlease Choose Another Name");
            } else if (saveAsName.equals("")) {
                JOptionPane.showMessageDialog(MainWindow.this,"Form Name Cannot Be Blank!");
            } else {
                File sourceFile = new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+selectedForm);
                File destinationFile = new File(curDir+"/Forms/"+host+"/"+MainWindow.selectedDatabase+"/"+saveAsName);
                try {
                    copyForm(sourceFile,destinationFile);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                myForms.add(saveAsName);
                jList1.setListData(myForms);
            }
        }
        buttonDelete.setEnabled(false);
        buttonOpen.setEnabled(false);
        buttonDesign.setEnabled(false);
        copy.setEnabled(false);
        delete.setEnabled(false);
        designView.setEnabled(false);
        export.setEnabled(false);
        openView.setEnabled(false);
        print.setEnabled(false);
        printPreview.setEnabled(false);
        rename.setEnabled(false);
        save.setEnabled(false);
    }
    
    protected void openView() {
        if (selectedForm.equals("Create Form by using Wizard")) {
            FormWizard formWizard = new FormWizard(myForms);
        } else if (selectedForm.equals("Create Form in Design View")) {
            createFormDesign();
        } else {
            OpenView.openView(selectedForm);
        }
    }
    
    protected void designView() {
        //Create And open a Form Wizard
        if (selectedForm.equals("Create Form by using Wizard")) {
            FormWizard formWizard = new FormWizard(myForms);
        } else if (selectedForm.equals("Create Form in Design View")) {
            createFormDesign();
        } else {
            OpenDesign.openDesign(selectedForm);
        }
    }
    
    protected void createFormDesign() {
        Vector availableTables=dbCon.getTables();
        if (availableTables.isEmpty()==false) {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension d=kit.getScreenSize();        
        MyFrame myFrame = new MyFrame(d);
        //set the Form's Title to Form #, where # is the last default form index number
        //got by substringing any form name that starts with Form
        int sizeForms = myForms.size();
        String str;
        int lastFormIndex=0;
        for (int i=0;i<sizeForms;i++) {
            str=(String)myForms.get(i);
            if (str.length()>4) {
                if (str.substring(0,4).equals("Form")) {
                    int lastIndex = Integer.parseInt(str.substring(5));
                    if (lastIndex > lastFormIndex) {
                        lastFormIndex=lastIndex;
                    }
                }
            }
        }
        lastFormIndex++;
        formTitle="Form "+lastFormIndex;
        myFrame.setTitle(formTitle);
        myFrame.flagNewDesign=true;
        myFrame.formatToolBar.refreshObjects();
        myFrame.formatToolBar.disableButtons();
        myFrame.setVisible(true);  
        new TableSelection(myFrame).setVisible(true);  
        } else {
            JOptionPane.showMessageDialog(null,"The selected Database doesn't contain any tables To create a Form from","Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    protected void setForms() {
        //Set the Forms List according to the Database selectedForm
        int indexForm = myForms.indexOf(selectedForm);
        if (myForms.isEmpty()==false) {
            myForms.removeAllElements();
        }
        if (! selectedDatabase.equals("")) {
            myForms.add("Create Form in Design View");
            myForms.add("Create Form by using Wizard");
            
            String curDir = System.getProperty("user.dir");
            File dir = new File(curDir+"/Forms/"+host+"/"+selectedDatabase);  //specify the directory
            String[] children = dir.list();  //get a list of the files in the directory
            
            if (children == null) {
                // Either directory does not exist or is not a directory
            } else {
                String filename;
                for (int i=0; i<children.length; i++) {
                    // Get filename of file or directory
                    filename = children[i];
                    myForms.add(filename);  //add the Name of the Form to the myForms Vector
                }
            }
            jList1.setListData(myForms);
            jList1.setSelectedIndex(indexForm);
            selectedForm=(String)jList1.getSelectedValue();
            if (jList1.getSelectedValue()==null) {
                buttonOpen.setEnabled(false);
                buttonDelete.setEnabled(false);
                buttonDesign.setEnabled(false);
                copy.setEnabled(false);
                delete.setEnabled(false);
                designView.setEnabled(false);
                export.setEnabled(false);
                openView.setEnabled(false);
                print.setEnabled(false);
                printPreview.setEnabled(false);
                rename.setEnabled(false);
                save.setEnabled(false);
            }
        } else {
            jList1.setListData(myForms);
        }
    }
    
    //protected void setForms(String )
    
    protected void setDatabases() {
        //Initialize the Tree
        DefaultTreeSelectionModel SelectionModel = new DefaultTreeSelectionModel();
        SelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.setSelectionModel(SelectionModel);
        root = makeTree();
        model = new DefaultTreeModel(root);
        jTree1.setModel(model);
        
        myDatabases=dbCon.getDatabases();  //get databases names and insert them into myDatabases Vector
        int size=myDatabases.size();  //get Number of databases (size of myDatabases Vector)
        for (int i=0;i<size;i++) {  //loop on myDatabases Vector and create a new tree node by each database name
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(myDatabases.get(i));
            model.insertNodeInto(newNode, root, root.getChildCount());
            TreeNode[] nodes = model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            jTree1.scrollPathToVisible(path);
        }
        if (! selectedDatabase.equals("")) {
            jTree1.setSelectionRow(selectedIndex+1);
            selectedDatabase=(String)myDatabases.get(selectedIndex);
        }
    }
    
    protected void arrangeByLastModified() {
        TreeMap<Long, String> sortedByModifiedForms = new TreeMap<Long ,String> (Collections.reverseOrder());
//Getting Files In Directory-------------------------------------
        String curDir = System.getProperty("user.dir");
        File dir = new File(curDir+"/Forms/"+host+"/"+selectedDatabase);
        
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            String filename;
            File file;
            for (int i=0; i<children.length; i++) {
                // Get filename of file or directory
                filename = children[i];
                file = new File(curDir+"/Forms/"+host+"/"+selectedDatabase+"/"+filename);
                sortedByModifiedForms.put(file.lastModified(),filename);
            }
        }
//end of getting files -----------------------------------------------------
        if (myForms.isEmpty()==false) {
            myForms.removeAllElements();
        }
        myForms.add("Create Form in Design View");
        myForms.add("Create Form by using Wizard");
        for (Long key:sortedByModifiedForms.keySet()) {
            myForms.add(sortedByModifiedForms.get(key));
        }
        jList1.setListData(myForms);
    }
    
    protected void arrangeByFirstCreated() {
        //Set the Forms List according to the Database selectedForm
        if (myForms.isEmpty()==false) {
            myForms.removeAllElements();
        }
        myForms.add("Create Form in Design View");
        myForms.add("Create Form by using Wizard");
        
//Getting Files In Directory-------------------------------------
        String curDir = System.getProperty("user.dir");
        File dir = new File(curDir+"/Forms/"+host+"/"+selectedDatabase);
        
        String[] children = dir.list();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            String filename;
            for (int i=0; i<children.length; i++) {
                // Get filename of file or directory
                filename = children[i];
                myForms.add(filename);
            }
        }
//end of getting files -----------------------------------------------------
        
        jList1.setListData(myForms);
    }
    
    protected void arrangeByName() {
        myForms.removeElementAt(0);
        myForms.removeElementAt(0);
        Collections.sort(myForms);
        myForms.add(0,"Create Form in Design View");
        myForms.add(1,"Create Form by using Wizard");
        jList1.setListData(myForms);
    }
    
    private class WindowHandler extends WindowAdapter {
        public void windowActivated(WindowEvent e) {
            if (flagStart==false)
            {
            setDatabases();
            setForms();
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Edit;
    private javax.swing.JMenu File;
    private javax.swing.JMenu Help;
    private javax.swing.JMenu Insert;
    private javax.swing.JMenu Options;
    private javax.swing.JMenuItem Settings;
    private javax.swing.JMenu View;
    private javax.swing.JMenuItem about;
    private javax.swing.JMenu arrange;
    private javax.swing.JButton buttonDelete;
    private javax.swing.JButton buttonDesign;
    private javax.swing.JButton buttonNew;
    private javax.swing.JButton buttonOpen;
    private javax.swing.JMenuItem byFirstCreated;
    private javax.swing.JMenuItem byLastModified;
    private javax.swing.JMenuItem byName;
    private javax.swing.JMenuItem copy;
    private javax.swing.JMenuItem delete;
    private javax.swing.JMenuItem designView;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenuItem export;
    private javax.swing.JMenuItem formByUsingWizard;
    private javax.swing.JMenuItem formInDesignView;
    private static javax.swing.JList jList1;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JTree jTree1;
    private javax.swing.JMenuItem newForm;
    private javax.swing.JMenuItem openView;
    private javax.swing.JMenuItem paste;
    private javax.swing.JMenuItem print;
    private javax.swing.JMenuItem printPreview;
    private javax.swing.JMenuItem rename;
    private javax.swing.JMenuItem save;
    // End of variables declaration//GEN-END:variables
    private class Login extends javax.swing.JFrame {           
        /** Creates new form Username */
        public Login() {
            initComponents();
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            setLocation((int)(dim.getWidth()-getWidth())/2, (int)dim.getHeight()/2-getHeight());                        
            setAlwaysOnTop(true); 
            setResizable(false);            
            MainWindow.this.setEnabled(false);
            setVisible(true);
        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField("127.0.0.1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MySQL Login");
        setAlwaysOnTop(true);
        jLabel1.setText("Username");

        jLabel2.setText("Password");

        jLabel3.setText("Host");
        
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldKeyPressed(evt);
            }
        });
        
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldKeyPressed(evt);
            }
        });
        
        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldKeyPressed(evt);
            }
        });

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });        

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2)
                    .add(jButton1)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                        .add(jButton2)
                        .add(jPasswordField1))
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2)))
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>
    
        private void jTextFieldKeyPressed(KeyEvent e) {
            if(jTextField1.getText().equals("")==false && jTextField2.getText().equals("")==false && e.getKeyChar() == KeyEvent.VK_ENTER){
                username = jTextField1.getText();
                password = jPasswordField1.getText();
                host = jTextField2.getText();
                try {
                    dbCon = new DBConnector(username, password, host);             
                    setDatabases();        
                    addWindowListener( new WindowHandler());
                    MainWindow.this.setEnabled(true);
                    MainWindow.this.repaint();
                    dispose();
                } catch(SQLException ex) {
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                    jTextField1.setText("");
                    jPasswordField1.setText("");                        
                    //ex.printStackTrace();
                }      
            }
        }       
        
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            System.exit(0);
        }
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            if(jTextField1.getText().equals("")==false && jTextField2.getText().equals("")==false) {
                username = jTextField1.getText();
                password = jPasswordField1.getText();
                host = jTextField2.getText();               
                try {
                    dbCon = new DBConnector(username, password, host);             
                    setDatabases();        
                    addWindowListener( new WindowHandler());
                    MainWindow.this.setEnabled(true);
                    MainWindow.this.repaint();
                    flagStart=false;
                    dispose();
                } catch(SQLException ex) {
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                    jTextField1.setText("");
                    jPasswordField1.setText("");                        
                    //ex.printStackTrace();
                }                   
            }
        }
                 
        /**
         * @param args the command line arguments
         */

        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JPasswordField jPasswordField1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration
    } 
    //###########################################################################
    public class TableSelection extends javax.swing.JFrame {
        MyFrame frame;
        /** Creates new form TableSelection */
        public TableSelection(MyFrame myFrame) {
            frame = myFrame;
            frame.setEnabled(false);
            setAlwaysOnTop(true);
            Toolkit kit = Toolkit.getDefaultToolkit();
            Dimension dim = kit.getScreenSize();            
            initComponents();            
            setLocation((int)(dim.getWidth()-getWidth())/2,(int)dim.getHeight()/2 - getHeight());

        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
        private void initComponents() {
            jLabel1 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox(dbCon.getTables());            
            
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Table Selection");
            setResizable(false);
            jLabel1.setText("Table");

            jButton1.setText("OK");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            jButton2.setText("Cancel");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });

            this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    dispose();
                    frame.dispose();
                }
            });
            
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(10, 10, 10)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
                        .add(jComboBox1, 0, 88, Short.MAX_VALUE))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel1)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 11, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton1)
                        .add(jButton2))
                    .addContainerGap())
            );
            pack();
        }// </editor-fold>

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
            frame.setTableName((String)jComboBox1.getSelectedItem());
            frame.setEnabled(true);
            dispose();
        }

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
            dispose();
            frame.dispose();
        }

        /**
         * @param args the command line arguments         
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TableSelection().setVisible(true);
                }
            });
        }
         */

        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration
    }     
}
